package com.bilibili.lzx.service.config;


import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;

/**
 * @author lzx
 * @date 2024/8/6 22:03
 */
@Configuration
public class JsonHttpMessageConverterConfig {

    public static void main(String[] args) {
        ArrayList<Object> strings = new ArrayList<>();
        Object o = new Object();
        strings.add(o);
        strings.add(o);
        System.out.println(strings);
        System.out.println(strings.size());
        System.out.println(JSONObject.toJSONString(strings));
        System.out.println(JSONObject.toJSONString(strings));
    }

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverter(){

        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastJsonConfig.setWriterFeatures(
                JSONWriter.Feature.PrettyFormat,
                JSONWriter.Feature.WriteNullStringAsEmpty,
                JSONWriter.Feature.WriteNullListAsEmpty,
                JSONWriter.Feature.WriteNulls,
                JSONWriter.Feature.MapSortField
        );
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(fastJsonHttpMessageConverter);
    }
}
